﻿/***************************************************************************************************/
/* @date on 2020-9-3 9:01:16                                                                       */
/* @author Xie Jian                                                                                */
/* @email 190954950@qq.com                                                                         */
/* @describe 着色器基类                                                                            */
/***************************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeColorHtml.LanguageCulour
{
    /// <summary>
    /// 着色器基类
    /// </summary>
    internal abstract class CoulourBase
    {
        /// <summary>
        /// 关键词链表
        /// </summary>
        protected List<string> _listSntistop = null;
        protected List<string> _listCode = null;
        /// <summary>
        /// 关键词是否小写
        /// </summary>
        protected bool isSntistopLaw = true;
        internal CoulourBase(string code)
        {
            _listCode = GetLines(code);
            IniSntistop();
        }

        /// <summary>
        /// 初始化关键词
        /// </summary>
        private void IniSntistop()
        {
            string sntistop = Sntistop();
            sntistop = sntistop.Replace("\r\n", "").Replace(" ", "");
            string[] array = sntistop.Split(',');
            List<string> list = new List<string>();
            foreach (string item in array)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    list.Add(item.Trim());
                }
            }
            _listSntistop = list;
        }

        /// <summary>
        /// 设置关键词
        /// </summary>
        protected abstract string Sntistop();

        /// <summary>
        /// 是否是关键词
        /// </summary>
        /// <returns></returns>
        protected bool IsSntistop(string text)
        {
            if (_listSntistop != null && _listSntistop.Count > 0)
            {
                string sntistop = text;
                if (!isSntistopLaw && !string.IsNullOrEmpty(sntistop))
                {
                    sntistop = sntistop.ToLower();
                }
                string temp = _listSntistop.Where(t => t == sntistop).FirstOrDefault();
                if (string.IsNullOrEmpty(temp))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }

        internal abstract string GetCodeColorHtml();

        /// <summary>
        /// 获取颜色
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        protected string GetColor(string text)
        {
            if (IsSntistop(text))
            {
                return "color:#0000ff;";
            }
            else
            {
                //return "color:#000055;";
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取注释颜色
        /// </summary>
        /// <returns></returns>
        protected string GetColorAnnotation()
        {
            return "color:#888888;";
        }

        /// <summary>
        /// 获取字符颜色
        /// </summary>
        /// <returns></returns>
        protected string GetColorCharacter()
        {
            return "color:#990000;";
        }

        #region 获取行
        /// <summary>
        /// 获取行
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private List<string> GetLines(string text)
        {
            List<string> list = new List<string>();
            string value = text.Replace("\r\n", "53CAC3848E924094BC9B55013F62FDE8");
            value = value.Replace("\n", "\r\n").Replace("53CAC3848E924094BC9B55013F62FDE8", "\r\n");
            while (!string.IsNullOrEmpty(value))
            {
                int index = value.IndexOf("\r\n");
                if (index >= 0)
                {
                    string item = value.Substring(0, index);
                    list.Add(item);
                    //list.Add("\r\n");
                    value = value.Substring(index + 2, value.Length - index - 2);
                }
                else
                {
                    list.Add(value);
                    value = string.Empty;
                }
            }
            return list;
        }
        #endregion

        #region 单词截分
        /// <summary>
        /// 获取行
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        protected List<string> GetWords(string text)
        {
            List<string> list = new List<string>();
            list = CutSymbol(text, "<,>,(,), ,.,;,[,]");
            return list;
        }
        #endregion

        #region 截断符号
        /// <summary>
        /// 截断符号
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private List<string> CutSymbol(string text, string symbols)
        {
            List<string> sumbolArray = symbols.Split(',').ToList();
            sumbolArray.Add(",");
            List<string> list = new List<string>();
            string value = text;
            while (!string.IsNullOrEmpty(value))
            {
                List<IndexKey> ikList = new List<IndexKey>();
                for (int i = 0; i < sumbolArray.Count; i++)
                {
                    int index = value.IndexOf(sumbolArray[i]);
                    if (index >= 0)
                    {
                        IndexKey ik = new IndexKey();
                        ik.Index = index;
                        ik.Key = sumbolArray[i];
                        ikList.Add(ik);
                    }
                }

                if (ikList != null && ikList.Count > 0)
                {
                    IndexKey ik = ikList.OrderBy(t => t.Index).First();
                    string item = value.Substring(0, ik.Index);
                    list.Add(item);
                    list.Add(ik.Key);
                    value = value.Substring(ik.Index + ik.Key.Length, value.Length - ik.Index - ik.Key.Length);
                }
                else
                {
                    list.Add(value);
                    value = string.Empty;
                }
            }
            return list;
        }
        #endregion

        private class IndexKey
        {
            public int Index { get; set; }
            public string Key { get; set; }
        }
    }
}
